home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 4 / Light ROM 4 - Disc 1.iso / text / maillist / 1994 / sep94.doc / 000544_owner-lightwave-l _Tue Sep 20 16:01:21 1994.msg < prev    next >
Internet Message Format  |  1995-03-23  |  11KB

  1. Return-Path: <owner-lightwave-l>
  2. Received: by mail2.netcom.com (8.6.9/Netcom)     id OAA22733; Tue, 20 Sep 1994 14:40:49 -0700
  3. Received: from pixar.com by mail2.netcom.com (8.6.9/Netcom)     id OAA22690; Tue, 20 Sep 1994 14:40:34 -0700
  4. Received: from dino.pixar.com by pixar.com with SMTP id AA04203   (5.65c/IDA-1.4.4 for <lightwave-l@netcom.com>); Tue, 20 Sep 1994 14:41:31 -0700
  5. Received: by dino.pixar.com (/\==/\ Smail3.1.25.1 #25.14)     id <m0qnCvU-00033jC@dino.pixar.com>; Tue, 20 Sep 94 14:40 PDT
  6. Message-Id: <m0qnCvU-00033jC@dino.pixar.com>
  7. Date: Tue, 20 Sep 94 14:40 PDT
  8. From: bjorke@pixar.com (Kevin Bjorke)
  9. To: lightwave-l@netcom.com
  10. Subject: Impatience rules
  11. Sender: owner-lightwave-l@netcom.com
  12. Precedence: bulk
  13. Reply-To: lightwave-l@netcom.com
  14.  
  15. Back when Stuart (JG?) posted the first metaforms "link" object, I was
  16. extremely impatient to see it, and 30 miles away from my amiga. So I quickly
  17. whipped-up the program below to use on my IRIS at work (Gee, I actually
  18. remembered the data format by heart!). It displays any LWOB and spins it
  19. around. I tried it with a few models, they all seemed happy. I came across
  20. this hack again this morning and thought someone on the list might have
  21. some use for it someday.
  22.  
  23. /****************************************************/
  24. /*** gLWOB.c ****************************************/
  25. /*** Very Minimal LWOB display for SGI IRIS GL (tm) */
  26. /*** Kevin Bjorke 11 July 94 ************************/
  27. /****************************************************/
  28. /*** Hit ESC to exit ********************************/
  29. /*** Hit 'P' to toggle points mode ******************/
  30. /*** Hit 'F' to toggle filled poly mode *************/
  31. /*** Arrows and ZERO to turn ************************/
  32. /****************************************************/
  33.  
  34. #include <stdio.h>
  35. #include <sys/types.h>
  36. #include <malloc.h>
  37. #include <memory.h>
  38. #include <math.h>
  39. #include <gl.h>
  40. #include <device.h>
  41.  
  42. #define READ_TAG(t)    fread((void *)(&t),sizeof(u_long),1,iff)
  43. #define READ_OFFSET(o)    fread((void *)(&o),sizeof(u_long),1,iff)
  44.  
  45. typedef float LWval;
  46. typedef struct { LWval x, y, z; } LWpt, *lpp;
  47.  
  48. FILE *iff;
  49.  
  50. lpp ptList=0L;
  51. u_long nPts=0;
  52. u_long nPols=0;
  53. u_short *refList=0L;
  54.  
  55. Boolean shoPoints=FALSE;
  56. Boolean shoPolys=FALSE;
  57. Boolean firstPoint=TRUE;
  58. Boolean knownCtr=FALSE;
  59.  
  60. LWpt minPt, maxPt, ctrPt;
  61. LWpt range;
  62. Coord wd;
  63.  
  64. #ifndef MAX
  65. #define MAX(a,b) (((a)<(b))?(b):(a))
  66. #define MIN(a,b) (((a)>(b))?(b):(a))
  67. #endif
  68.  
  69. /*********************************/
  70. /*********************************/
  71. /*********************************/
  72.  
  73. static char _junk[8];
  74. char *tag_str(u_long t)
  75. {
  76.     char *cp, *tcp;
  77.     int i;
  78.     tcp = (char *)(&t);
  79.     cp = _junk;
  80.     for (i=0; i<4; ++i) *cp++ = *tcp++;
  81.     _junk[4] = '\0';
  82.     return(_junk);
  83. }
  84.  
  85. /*********************************/
  86. /*********************************/
  87. /*********************************/
  88.  
  89. read_pnts(u_long o)
  90. {
  91.     LWpt p;
  92.     u_long i;
  93.     lpp pp;
  94.     nPts = o/sizeof(LWpt);
  95.     printf("%lu Points\n",nPts);
  96.     if (ptList) free((char *)ptList);
  97.     ptList = (lpp)malloc(nPts*sizeof(LWpt));
  98.     if (!ptList) {
  99.     printf("boink\n");
  100.     exit(4);
  101.     }
  102.     for (i=0,pp=ptList; i<nPts; ++i,++pp) {
  103.     fread((void *)(&p),sizeof(LWval),3,iff);
  104.     /* printf("P%4lu: %g %g %g\n",i,p.x,p.y,p.z); */
  105.     pp->x = p.x;
  106.     pp->y = p.y;
  107.     pp->z = p.z;
  108.     if (firstPoint) {
  109.         firstPoint = FALSE;
  110.         maxPt.x = minPt.x = p.x;
  111.         maxPt.y = minPt.y = p.y;
  112.         maxPt.z = minPt.z = p.z;
  113.     } else {
  114.         minPt.x = MIN(minPt.x,p.x); maxPt.x = MAX(maxPt.x,p.x);
  115.         minPt.y = MIN(minPt.y,p.y); maxPt.y = MAX(maxPt.y,p.y);
  116.         minPt.z = MIN(minPt.z,p.z); maxPt.z = MAX(maxPt.z,p.z);
  117.     }
  118.     }
  119.     return(o);
  120. }
  121.  
  122. read_pols(u_long o)
  123. {
  124.     int i, j, c;
  125.     nPols = o/(sizeof(u_short));
  126.     printf("%lu polygon refs\n",nPols);
  127.     if (refList) free((char *)refList);
  128.     refList = (u_short *)malloc((size_t)o);
  129.     fread((void *)refList,1,o,iff);
  130. #ifdef YAK
  131.     for (i=0; i<nPols; ) {
  132.     c = refList[i++];
  133.     printf("[%d] ",c);
  134.     for (j=0; j<=c; ++j) {
  135.         printf("%u ",refList[i++]);
  136.     }
  137.     printf("\n");
  138.     }
  139.     printf("\n");
  140. #endif
  141.     return(o);
  142. }
  143.  
  144. /*********************************/
  145. /*********************************/
  146. /*********************************/
  147.  
  148. read_surf(u_long o)
  149. {
  150.     char *a;
  151.     a = malloc((size_t)o);
  152.     if (!a) return(0);
  153.     fread((void *)a,1,o,iff);
  154.     printf("Surf \"%s\"\n",a);
  155.     free(a);
  156.     return(o);
  157. }
  158.  
  159. /*********************************/
  160. /*********************************/
  161. /*********************************/
  162.  
  163. read_srfs(u_long o)
  164. {
  165.     char *a;
  166.     a = malloc((size_t)o);
  167.     if (!a) return(0);
  168.     fread((void *)a,1,o,iff);
  169.     printf("Srfs \"%s\"\n",a);
  170.     free(a);
  171.     return(o);
  172. }
  173.  
  174. /*********************************/
  175. /*********************************/
  176. /*********************************/
  177.  
  178. #define BS (sizeof(u_long)+sizeof(u_long))
  179. u_long read_chunk(void)
  180. {
  181.     int e;
  182.     u_long tt;
  183.     u_long o;
  184.     if (!(e=READ_TAG(tt)))    return(0);
  185.     if (!(e=READ_OFFSET(o)))    return(0);
  186.     switch (tt) {
  187.     case 'PNTS':    return(BS+read_pnts(o)); break;
  188.     case 'SURF':    return(BS+read_surf(o)); break;
  189.     case 'SRFS':    return(BS+read_srfs(o)); break;
  190.     case 'POLS':    return(BS+read_pols(o)); break;
  191.     default:
  192.     printf("Chunk %s size %lu\n",tag_str(tt),o);
  193.     fseek(iff,(long)o,SEEK_CUR);
  194.     return(BS+o);
  195.     }
  196. }
  197.  
  198. /*********************************/
  199. /*********************************/
  200. /*********************************/
  201.  
  202. void lw_vertex(lpp pt)
  203. {
  204.     static float vv[3];
  205.     float d;
  206.     vv[0] = pt->x;
  207.     vv[1] = pt->y;
  208.     vv[2] = pt->z;
  209.     v3f(vv);
  210. }
  211.  
  212. /*********************************/
  213. /*********************************/
  214. /*********************************/
  215.  
  216. void calc_window()
  217. {
  218.     if (!knownCtr) {
  219.     ctrPt.x = (minPt.x + maxPt.x)/2.0;
  220.     ctrPt.y = (minPt.y + maxPt.y)/2.0;
  221.     ctrPt.z = (minPt.z + maxPt.z)/2.0;
  222.     fprintf(stderr,"Ctr %g %g %g\n",ctrPt.x,ctrPt.y,ctrPt.z);
  223.     range.x = (maxPt.x - minPt.x);
  224.     range.y = (maxPt.y - minPt.y);
  225.     range.z = (maxPt.z - minPt.z);
  226.     wd = MAX(range.x,range.y);
  227.     wd = MAX(wd,range.z);
  228.     wd *= (1.05/2.);
  229.     knownCtr = TRUE;
  230.     }
  231.     ortho(-wd,wd, -wd,wd, -wd,wd);
  232. }
  233.  
  234. /*********************************/
  235. /*********************************/
  236. /*********************************/
  237.  
  238. #define INCR    0x02
  239. #define BASE    0x20
  240. #define LIMT    0xFF
  241.  
  242. #define RNC    .03
  243.  
  244. void loopy(void)
  245. {
  246.     u_long i, j, c, f;
  247.     lpp pp;
  248.     u_short *r, n;
  249.     u_long rCtr, gCtr, bCtr, compo;
  250.     long dev;
  251.     short val;
  252.     float rx=(RNC*7.);
  253.     float ry=(RNC*4.);
  254.     float rpx=0.0;
  255.     float rpy=0.0;
  256.     Boolean first=TRUE;
  257.     for(;;) {
  258.     if ((rx!=0.)||(ry!=0.0)||(first)) {
  259.         first = FALSE;
  260.         cpack(0L);
  261.         czclear(0L,getgconfig(GC_ZMAX));
  262.         calc_window();
  263.         rot(rpx,'x');
  264.         rot(rpy,'y');
  265.         rpx += rx;
  266.         rpy += ry;
  267.         translate((Coord)-ctrPt.x,(Coord)-ctrPt.y,(Coord)-ctrPt.z);
  268.         if (refList && !shoPoints) {
  269.         if (shoPolys) {
  270.             cpack(0xFFFF0000);
  271.             rCtr = BASE;
  272.             gCtr = (BASE<<8);
  273.             bCtr = (BASE<<16);
  274.             for (i=0; i<nPols; ) {
  275.             c = refList[i++];
  276.             f = refList[i];
  277.             compo = 0xFF000000 | bCtr | gCtr | rCtr;
  278.             cpack(compo);
  279.             rCtr += INCR;
  280.             if (rCtr > LIMT) {
  281.                 rCtr = BASE;
  282.                 gCtr += (INCR<<8);
  283.                 if (gCtr > (LIMT<<8)) {
  284.                 gCtr = (BASE<<8);
  285.                 bCtr += (INCR<<16);
  286.                 if (bCtr > (LIMT<<16)) {
  287.                     bCtr = (BASE<<16);
  288.                 }
  289.                 }
  290.             }
  291.             if (c>1) {
  292.                 bgnpolygon();
  293.                 for (j=0; j<c; ++j)
  294.                     lw_vertex(&ptList[refList[i++]]);
  295.                 endpolygon();
  296.             } else {
  297.                 if (c>1) {
  298.                 bgnline();
  299.                     for (j=0; j<c; ++j)
  300.                     lw_vertex(&ptList[refList[i++]]);
  301.                     lw_vertex(&ptList[f]);
  302.                 endline();
  303.                 } else {
  304.                 bgnpoint();
  305.                     lw_vertex(&ptList[refList[i++]]);
  306.                 endpoint();
  307.                 }
  308.             }
  309.                 ++i;    /* skip surface index */
  310.             }
  311.         } else {
  312.             cpack(0xFF0000FF);
  313.             for (i=0; i<nPols; ) {
  314.             c = refList[i++];
  315.             f = refList[i];
  316.             if (c>1) {
  317.                 bgnline();
  318.                 for (j=0; j<c; ++j)
  319.                     lw_vertex(&ptList[refList[i++]]);
  320.                 lw_vertex(&ptList[f]);
  321.                 endline();
  322.             } else {
  323.                 bgnpoint();
  324.                 lw_vertex(&ptList[refList[i++]]);
  325.                 endpoint();
  326.             }
  327.                 ++i;    /* skip surface index */
  328.             }
  329.         }
  330.         } else {
  331.         cpack(0xFFFFFFFF);
  332.         for (i=0,pp=ptList; i<nPts; ++i,++pp) {
  333.             bgnpoint();
  334.             lw_vertex(pp);
  335.             endpoint();
  336.         }
  337.         }
  338.         swapbuffers();
  339.         first=FALSE;
  340.     }
  341.     rx = ((rx<(RNC/2.))&&(rx>(-RNC/2.))) ? 0. : rx;
  342.     ry = ((ry<(RNC/2.))&&(ry>(-RNC/2.))) ? 0. : ry;
  343.     if (qtest()||((rx==0.)&&(ry==0.0))) {
  344.         switch(dev = qread(&val)) {
  345.         case REDRAW:
  346.         reshapeviewport();
  347.         calc_window();
  348.         first=TRUE;
  349.         break;
  350.         case FKEY:
  351.         if (val) {
  352.             shoPolys = !shoPolys;
  353.             if (shoPolys) shoPoints=FALSE;
  354.             first=TRUE;
  355.         }
  356.         break;
  357.         case PKEY:
  358.         if (val) {
  359.             shoPoints = !shoPoints;
  360.             if (shoPoints) shoPolys=FALSE;
  361.             first=TRUE;
  362.         }
  363.         break;
  364.         case UPARROWKEY:
  365.         if (val) rx += RNC;
  366.         break;
  367.         case DOWNARROWKEY:
  368.         if (val) rx -= RNC;
  369.         break;
  370.         case LEFTARROWKEY:
  371.         if (val) ry += RNC;
  372.         break;
  373.         case RIGHTARROWKEY:
  374.         if (val) ry -= RNC;
  375.         break;
  376.         case ZEROKEY:
  377.         if (val) {
  378.             if ((rx==0.)&&(ry==0.)) {
  379.             rpx = rpy = 0.;
  380.             first = TRUE;
  381.             } else {
  382.             rx = ry = 0.;
  383.             }
  384.         }
  385.         break;
  386.         case ESCKEY:
  387.         case DELKEY:
  388.         exit(0);
  389.         break;
  390.         }
  391.     }
  392.     }
  393. }
  394.  
  395. /*********************************/
  396. /*********************************/
  397. /*********************************/
  398.  
  399. main(int argc,char *argv[])
  400. {
  401.     u_long t, ct;
  402.     u_long o, s, c;
  403.     if (argc<2) {
  404.     fprintf(stderr,"File name, please\n");
  405.     fprintf(stderr,"\t'P' toggles points\n");
  406.     fprintf(stderr,"\t'F' toggles Filled Polys\n");
  407.     fprintf(stderr,"\tArrows and ZERO to turn\n");
  408.     fprintf(stderr,"\tZERO twice to reset\n");
  409.     fprintf(stderr,"\tESC to Quit\n");
  410.     exit(2);
  411.     }
  412.     iff = fopen(argv[1],"r");
  413.     if (!iff) {
  414.     fprintf(stderr,"Bad File Name \"%s\"\n",argv[1]);
  415.     exit(3);
  416.     }
  417.     READ_TAG(t);
  418.     READ_OFFSET(o);
  419.     READ_TAG(ct);
  420.     if (t != 'FORM') {
  421.     fprintf(stderr,"Not a FORM\n");
  422.     fclose(iff);
  423.     exit(3);
  424.     }
  425.     printf("FORM %s size %lu\n",tag_str(ct),o);
  426.     s = 0;
  427.     while (s<o) {
  428.     c = read_chunk();
  429.     if (!c) break;
  430.     s += c;
  431.     }
  432.     fclose(iff);
  433.     if (ptList) {
  434.     foreground();
  435.     keepaspect(1,1);
  436.     winopen("gLWOB");
  437.     calc_window();
  438.     doublebuffer();
  439.     zbuffer(TRUE);
  440.     RGBmode();
  441.     gconfig();
  442.     qdevice(ESCKEY);
  443.     qdevice(PKEY);
  444.     qdevice(FKEY);
  445.     qdevice(UPARROWKEY);
  446.     qdevice(DOWNARROWKEY);
  447.     qdevice(LEFTARROWKEY);
  448.     qdevice(RIGHTARROWKEY);
  449.     qdevice(ZEROKEY);
  450.     loopy();
  451.     free((char *)ptList);
  452.     }
  453.     if (refList) free((char *)refList);
  454. }
  455.  
  456. /*********************************/
  457. /************************* eof ***/
  458. /*********************************/
  459. -------------------------------------------------------------------------- 
  460.   Kevin Bjorke         |  If you draw a picture, and then draw more and
  461.   Animation Scientist  |  more pictures, and put them on TV, then dino-
  462.   Hi Tech Toons        |  saurs will turn into birds! - Rebecca